/* eslint-disable @typescript-eslint/camelcase */
/**
 * Created by zhangsong on 2019-06-01.
 */
import moment from 'moment';
import { datetimeFormat } from '@/constants/styleConfig';
import globalConfig from '@/constants/config';
import URI from '@/services/uri';
import _ from 'lodash';
import {
  requestPostByToken,
  requestGetByToken,
  requestPutByToken,
  requestDeleteByToken,
} from '@/utils/request';

const url = URI.application;

const setDataModify = list => {
  let arr = [];
  arr = Array.isArray(list)
    ? list.map((item, index) => {
      item.url = `${globalConfig.host}${globalConfig.baseAPI}${item.url}`;
      item.name = item.url.substring(item.url.lastIndexOf('/') + 1);
      item.uid = (index + 1) * -1;
      item.status = 'done';
      return item;
    })
    : []
  return arr;
}

const setDataList = list => {
  let annexesBase64 = [];
  if (Array.isArray(list)) {
    annexesBase64 = list.map(item => item.url || item.thumbUrl);
  }

  return annexesBase64
}

const setNewDataList = list => {
  let annexesBase64 = [];
  if (Array.isArray(list)) {
    annexesBase64 = list.map(item => item.thumbUrl);
  }
  return annexesBase64
}

// 有关于多选的东西增删改查
export const modifycontractorSelete = body =>
  requestPostByToken({
    url: url.creatselete.modifycontractor.replace('{id}', body.id),
    body: { contract_number: body.data },
  });

export const deletecontractorSelete = id =>
  requestDeleteByToken({
    url: url.creatselete.detelecontractor.replace('{id}', id),
  });

//  访问地区的部分
export const modifylocationSelete = body =>
  requestPostByToken({
    url: url.creatselete.modifylocation.replace('{id}', body.id),
    body: { location_name: body.data },
  });

export const deletelocationSelete = id =>
  requestDeleteByToken({
    url: url.creatselete.detelelocation.replace('{id}', id),
  });

// 分组的部分
export const modifygroupSelete = body =>
  requestPostByToken({
    url: url.creatselete.detelegroup.replace('{id}', body.id),
    body: { name: body.data },
  });

export const deletegroupSelete = id =>
  requestDeleteByToken({
    url: url.creatselete.detelegroup.replace('{id}', id),
  });

export const registerEface = params =>
  requestPostByToken({
    url: url.person.registerEface,
    body: params,
  });

// 强制将记录更新为登记成功
export const forceRegisterSuccess = id =>
  requestPutByToken({
    url: url.person.forceUpdateSuccess,
    body: {
      contractorId: id,
    },
  });

export const listSubcontractor = () =>
  requestGetByToken({
    url: url.temporaryUrl.listSubcontractor,
  });

/**
 * 审核通过
 * @param id
 */
export const approval = id =>
  requestGetByToken({
    url: url.temporaryUrl.approval.replace('{id}', id),
  });

// 更新註銷日期
export const updateCallDate = async param => {
  const response = await requestPostByToken({
    url: url.temporaryUrl.modifyacll,
    body: param,
  });
  return response;
}

// 获取group list
export const getGroupList = async () => {
  const response = await requestPostByToken({
    url: URI.common.getgrouplist,
    body: {},
  });
  return response;
}


// 新增group list
export const creatGroupList = async param => {
  const response = await requestPostByToken({
    url: URI.common.creatgrouplist,
    body: param,
  });
  return response;
}

// 獲取主合同變編號的list
export const getMainContractor = async () => {
  const response = await requestGetByToken({
    url: URI.application.temporaryUrl.getMainContractor,
  });
  return response;
}

export const createMainContractor = async data => {
  const response = await requestPostByToken({
    url: URI.application.temporaryUrl.createMainContractor,
    body: { contract_number: data },
  });
  return response;
}

// 獲取訪問地區list
export const getLocationList = async () => {
  const response = await requestGetByToken({
    url: URI.application.temporaryUrl.getloction,
  });
  return response;
}

export const createLocation = async data => {
  const response = await requestPostByToken({
    url: URI.application.temporaryUrl.creatlocation,
    body: { location_name: data },
  });
  return response;
}

// 审核不通过
export const disapproval = id =>
  requestGetByToken({
    url: url.temporaryUrl.disapproval.replace('{id}', id),
  });

export const getPDFById = id =>
  requestGetByToken({
    url: url.temporaryUrl.pdf.replace('{id}', id),
  });

export const contractorDownXlsx = () =>
  requestGetByToken({
    url: url.temporaryUrl.downxlsx,
  });

export const listByParam = async query => {
  const {
    identity_card,
    pageSize,
    page,
    card_type,
    checkedSpecified,
    is_expire,
    pass_no,
    name,
    ...other
  } = query;
  const users = JSON.parse(localStorage.getItem('user'));
  console.log('1111111', query, users)
  const result = await requestGetByToken({
    url: _.get(users, 'role_id') === '110' ? url.temporaryUrl.permissions : url.temporaryUrl.newlist,
    query: {
      ...query,
    },
  });
  const rows = result.data.map(item => ({
    ...item,
    card_typestr: item.card_type === 1 ? '现场申请临时卡' : '预先申请临时卡',
    fileGroupId: item.file_group_id,
    createTime: item.create_time,
    updateTime: item.update_time,
    id: item.id,
    surname: item.surname,
    otherName: item.other_names,
    sex: item.sex,
    nameAlias: item.name_alias,
    commercialCodeAlias: item.commercial_code_alias,
    identityCard: item.identity_card,
    birthday: item.birthday,
    placeOfBirth: item.place_of_birth,
    nationality: item.nationality, // 国籍
    address: item.address,
    telephone: item.telephone,
    telephoneSpare: item.telephone_spare,
    // 紧急联系人
    emergencyContactedName: item.emergency_contacted_name,
    emergencyContactedRelationship: item.emergency_contacted_relationship,
    emergencyContactedAddress: item.emergency_contacted_address,
    emergencyContactedTelephone: item.emergency_contacted_telephone,

    // partB
    contractNumber: item.contract_number, // 主分包商编号
    contractPosition: item.contract_position,
    contractSubcontractor: item.contract_subcontractor,
    contractEmployer: item.contract_employer,
    contractFrom: item.contract_from, // 合同开始时间
    contractTo: item.contract_to, // 合同结束时间
    contractResident: item.has_resident,
    contractApplicantDrive: item.need_drivng, // 是否申请车辆通行证
    contractVehicle: item.contract_vehicle, // 车牌号

    // partC
    checkedApproved: item.checked_approved,
    checkedClassification: item.checked_classification,
    checkedSpecified: item.checked_specified,
    checkedExpiry: item.checked_expiry, // 申请过期时间
    checkedReasons: item.checked_reasons, // 申请拒绝原因
    cardFront: item.card_front,
    cardBack: item.card_back,
    // 头像
    // headPhoto: item.photograph_url,
    recoimage: `${globalConfig.host}${globalConfig.baseAPI}${item.qrcode_url}`,
    annexes: item.annex_list,
    efaceStatus: item.eface_status,

    datetwo: `${item.start_time} — ${item.expire_time}`,
  }));
  console.log('臨時卡位置1111', query, rows)
  return {
    data: rows,
    page: result.page_num,
    pageSize: result.page_size,
    total: result.total,
  };
};

export const getById = async id => {
  const response = await requestGetByToken({
    url: url.temporaryUrl.getById.replace('{id}', id),
  });
  console.log('這是二維碼的', response)
  const { data } = response;
  const res = {
    ...data,
    contract_number: { value: data.contract_number, label: data.contract_number },
    id_base64: setDataModify(data.id_list),
    green_base64: setDataModify(data.green_list),
    drive_base64: setDataModify(data.drive_list),
    regist_base64: setDataModify(data.regist_list),
    vacc_base64: setDataModify(data.vacc_list),
    exemption_base64: setDataModify(data.exemption_list),
    recoimage: `${globalConfig.host}${globalConfig.baseAPI}${data.qrcode_url}`,
  }
  return res;
};

export const saveContractor = async data => {
  const {
    id_base64,
    green_base64,
    drive_base64,
    regist_base64,
    vacc_base64,
    exemption_base64,
  } = data;
  const res = {
    ...data,
    contract_number: data?.contract_number?.label,
    expire_time: moment(data.expire_time).format(datetimeFormat.time),
    id_base64: setNewDataList(id_base64),
    green_base64: setNewDataList(green_base64),
    drive_base64: setNewDataList(drive_base64),
    regist_base64: setNewDataList(regist_base64),
    vacc_base64: setNewDataList(vacc_base64),
    exemption_base64: setNewDataList(exemption_base64),
  }
  console.log('保存的數據', res, data)
  const response = await requestPostByToken({
    url: url.temporaryUrl.save,
    body: res,
  });
  return response;
};

export const removeById = id =>
  requestDeleteByToken({
    url: url.temporaryUrl.removeById.replace('{id}', id),
  });

export const outById = id =>
  requestGetByToken({
    url: url.temporaryUrl.outById.replace('{id}', id),
  });

export const updateById = ({ id, body }) => {
  const {
    id_base64,
    green_base64,
    drive_base64,
    regist_base64,
    vacc_base64,
    exemption_base64,
  } = body;
  const res = {
    ...body,
    contract_number: body?.contract_number?.label,
    expire_time: moment(body.expire_time).format(datetimeFormat.time),
    id_base64: setDataList(id_base64),
    green_base64: setDataList(green_base64),
    drive_base64: setDataList(drive_base64),
    regist_base64: setDataList(regist_base64),
    vacc_base64: setDataList(vacc_base64),
    exemption_base64: setDataList(exemption_base64),
  }
  console.log('是的是的', res, body)
  return requestPutByToken({
    url: url.temporaryUrl.modifyById.replace('{id}', id),
    body: res,
  });
};
